//
//  NavPageViewController.m
//  YYZB
//
//  Created by BY_R on 16/4/1.
//
//

#import "NavPageViewController.h"
#import "LogViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface NavPageViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scrollview;
@property(nonatomic,strong)UIPageControl * pageControl;
@end

@implementation NavPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.scrollview.pagingEnabled = YES;
    self.scrollview.delegate = self;
    self.scrollview.contentSize = CGSizeMake(SCREEN_WIDTH*4, SCREEN_HEIGHT);
    
    for (int i = 0; i <5; i++)
    {
        UIImageView * imageView =[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSString * str = [NSString stringWithFormat:@"服务端引导页%d.png",i+1];
        imageView.image = [UIImage imageNamed:str];
        imageView.tag = i;
        [self.scrollview addSubview:imageView];
        
        if (imageView.tag  == 3) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(SCREEN_WIDTH/2-100, 0, 200, SCREEN_HEIGHT);
            button.layer.cornerRadius = 6;
            //[button setBackgroundImage:[UIImage imageNamed:@""] forState:0];
            [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];
            imageView.userInteractionEnabled = YES;
        }
    }
    self.scrollview.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:self.scrollview];
    
        self.pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 10)];
        self.pageControl.currentPageIndicatorTintColor=[UIColor colorWithRed:0.153 green:0.533 blue:0.796 alpha:1.0];
    [self.pageControl addTarget:self action:@selector(chagePage) forControlEvents:UIControlEventValueChanged];
    
//        [self.view addSubview:self.pageControl];
        self.pageControl.numberOfPages=4;
}
-(void)click{
    LogViewController * vc = [[LogViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
//    [self presentViewController:nav animated:YES completion:nil];
    //[self.navigationController pushViewController:vc animated:YES];
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
        CGFloat pageWidth = CGRectGetWidth(self.scrollview.frame);
        NSInteger page = floor((self.scrollview.contentOffset.x -pageWidth/2)/pageWidth) +1;
        self.pageControl.currentPage = page;
    
//        [self loadScrollViewPage:page-1];
//        [self loadScrollViewPage:page];
//        [self loadScrollViewPage:page+1];
}
-(void)chagePage{
    NSInteger page = self.pageControl.currentPage;
    CGRect bounds = self.scrollview.bounds;
    bounds.origin.x = CGRectGetWidth(bounds) * page;
    bounds.origin.y = 0;
    [self.scrollview scrollRectToVisible:bounds animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
